package com.ywb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywb.entity.EduSubject;
import com.ywb.entity.subject.FirstSubject;
import com.ywb.entity.subject.SecondSubject;
import com.ywb.service.EduSubjectService;
import org.apache.velocity.util.ArrayListWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author ywb
 * @since 2021-05-27
 */
@Controller
@RequestMapping("/subject")
public class EduSubjectController extends BaseController{

    @Autowired
    private EduSubjectService subjectService;

    @RequestMapping("/list")
    public String toSubjectList(Model model){
        List<FirstSubject> firstSubjects = subjectService.getFirstSubjects();

        model.addAttribute("list", firstSubjects);
        model.addAttribute("pageName", "subject/list.jsp");
        return "index";
    }

    /**
     * 路由跳转
      * @param model
     * @return
     */
    @GetMapping("/add")
    public String add(Model model){
        model.addAttribute("pageName", "subject/add.jsp");
        return "index";
    }
//    获取用户上传的文件
    @PostMapping("/addSubject")
    public String toSubjectSave(@RequestParam("file")MultipartFile file, Model model){
//        逻辑处理交由service负责
        subjectService.saveSubject(file, subjectService);

        model.addAttribute("pageName", "subject/list.jsp");
        return "redirect:/subject/list";
    }

    @RequestMapping("/deleteById/{id}")
    public String deleteSubject(Model model,@PathVariable String id){
        subjectService.removeById(id);

        model.addAttribute("pageName", "subject/list.jsp");
        return "redirect:/subject/list";
    }


    @PostMapping("/updateSubject")
    public String updateSubject(@RequestParam String id,
                                @RequestParam String title,
                                Model model){
        subjectService.updateById(new EduSubject(id, title));

        model.addAttribute("msg", "更新成功");
        model.addAttribute("pageName", "subject/list.jsp");
        return "redirect:/subject/list";
    }

    @GetMapping("/getSecondSubjectsById/{parentId}")
    @ResponseBody
    public List<EduSubject> getSecondSubjectsById(@PathVariable String parentId){
        List<EduSubject> list = subjectService.getSecondSubjectById(parentId);
        return list;
    }

}

